import axios from 'axios'

// axios.defaults.baseURL = 'https://api.example.com';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';


const request = ({
    url,
    params,
    headers,
    data,
    method
}) =>{
    return new Promise((resolve,reject) => {
        console.log('pro')
        switch(method.toUpperCase()){
            case 'GET':
            axios({
                url,
                method,
                params
            }).then( res => resolve( res ) )
            .catch( err => reject( err ) )
            break;
            case 'POST':
                const p = new URLSearchParams()
                if(data){
                    for(var key in data){
                        p.append(key ,data[key])
                    }
                }
            axios({
                url,
                method,
                data:p,
                headers:{}
            }).then( res => resolve( res ) )
            .catch( err => reject( err ) )
            break;
        }
    })
}

export default request